package com.passer.simpletransfer.websocket.vo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.passer.simpletransfer.websocket.contant.WebSocketMessageType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WebSocketMessageVo<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -3570419598317960871L;

    private WebSocketMessageType type;

    private T data;

    /**
     * 消息发送结果
     */
    private boolean succeed;

    public String formatToJsonString() throws JsonProcessingException {
        String messageJsonString;
        ObjectMapper objectMapper = new ObjectMapper();
        messageJsonString = objectMapper.writeValueAsString(this);
        return messageJsonString;
    }

}
